<?php
namespace App\Http\Controllers\ApiQd;

use App\Service\ApiQd\GoodsService;
use Illuminate\Http\Request;

class GoodsController extends BaseController
{
    //商品

    public function __construct()
    {
        $this->db = new GoodsService();
    }

    //分类列表
    public function getCateList(Request $request)
    {
        $token = $request->post('token', '');
        $this->db->getUserByToken($token);
        $res = $this->db->getCateList();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //商品列表
    public function getGoodsList(Request $request)
    {
        $token = $request->post('token', '');
        $user = $this->db->getUserByToken($token);
        if (!$user) {
            return $this->toJson(-2, '没有登录');
        }
        $uid = $user['id'];
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 4);
        $cateid = $request->post('cate_id', 0);
        $keyword = $request->post('keyword', '');
        $pas['is_sale'] = $request->post('is_sale', 0); //0没有，1降序，2升序
        $pas['is_price'] = $request->post('is_price', 0); //0没有，1降序，2升序
        $pas['is_new'] = $request->post('is_new', 0); //0没有，1上新
        $pas['is_in_reason'] = $request->post('is_in_reason', 0); //0非当季，1当季
        if ($keyword) {
            $this->db->addKeyword($keyword, $uid);
        }
        $res = $this->db->getGoodsList($pages, $cateid, $keyword ,$pas, $user);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //商品选项
    public function getOption(Request $request)
    {
        $token = $request->post('token', '');
        $this->db->addApiCount($token);
        $res = $this->db->getOption();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);

    }

    //商品详情
    public function getGoodsOne(Request $request)
    {
        $token = $request->post('token', '');
        $user = $this->db->getUserByToken($token);
        $gid = $request->post('gid', 0);
        if (!$gid) {
            return $this->toJson(-2, '商品ID必须的');
        }
        $res = $this->db->getGoodsOne($gid, $user);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //关键词列表
    public function getKeywordList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $res = $this->db->getKeywordList($uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //关键词删除
    public function keywordBatchDel(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $res = $this->db->keywordBatchDel($uid);
//        if (!$res) {
//            return $this->toJson(-2, '操作失败');
//        }
        return $this->toJson();
    }

    //SKU列表
    public function getSkuList(Request $request)
    {
        $token = $request->post('token', '');
        $user = $this->db->getUserByToken($token);
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 4);
        $gid = $request->post('gid', 0);
        if (!$gid) {
            return $this->toJson(-2, '商品ID必须的');
        }
        $res = $this->db->getSkuList($pages, $gid, $user);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //浏览记录
    public function readAdd(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        if (!$uid) {
            return $this->toJson(-2, '用户不存在');
        }
        $gid = $request->post('gid', 0);
        $sku_id = $request->post('sku_id', 0);
        if (!$gid) {
            return $this->toJson(-2, '商品ID必须的');
        }
        $res = $this->db->setRead($uid, $gid, $sku_id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0);
    }

    //浏览历史
    public function getReadList(Request $request)
    {
        $token = $request->post('token', '');
        $user = $this->db->getUserByToken($token);
//        ddd(toToday(), toWeek(), toMonth());
        $res = ['day' => [], 'week' => [], 'month' => []];
        $days = $this->db->getTraceList($user, toToday());
        $weeks = $this->db->getTraceList($user, toWeek());
        $months = $this->db->getTraceList($user, toMonth());
        if ($days['code'] == 0) {
            $res['day'] = $days['data'];
        }
        if ($weeks['code'] == 0) {
            $res['week'] = $weeks['data'];
        }
        if ($months['code'] == 0) {
            $res['month'] = $months['data'];
        }
//        $res = $this->db->getTraceList($uid);
//        if ($res['code'] < 0) {
//            return $this->toJson(-2, $res['msg']);
//        }
        return $this->toJson(0, '', $res);
    }
}